require('../models/publish_cfg.js');
var _ = require('lodash');
var mongoose = require('mongoose');
var PublishConfig = mongoose.model('PublishConfig');
var ProductConfig = mongoose.model('ProductConfig');
var Product = mongoose.model('Product');
var EventProxy = require('eventproxy');
var url = require('url');


exports.all = function (cb) {
    PublishConfig.find({}, cb);
};

exports.addPublishConfig = function (config_info, cb) {
    var config = new PublishConfig(config_info);
    config.save(cb);
};

exports.deletePublishConfig = function (config_ids, cb) {
    PublishConfig.remove({_id: {$in: config_ids}}, cb);
};

exports.getPublishConfig = function (config_id, cb) {
    PublishConfig.findOne({_id: config_id}, cb);
};

exports.editPublishConfig = function (config_id, config_info, cb) {
    PublishConfig.update({_id: config_id}, {
        name: config_info.name,
        type: config_info.type,
        extend_info: config_info.extend_info,
        product_configs: config_info.product_configs
    }, cb);
};


//根据发布配置id获取发布商品的信息（extend_info） 商品名（name）  商品类型(type) 包含产品信息（product_info）
exports.getGoodsInfo = function (config_id, cb) {
    //从publish_cfg中找到product_configs，根据product_configs获取产品信息
    PublishConfig.findOne({_id: config_id}, function (err, doc) {
        var extend_info = doc.extend_info;
        var name = doc.name;
        var type = doc.type;
        var productsInfo = [];
        var goodsInfo = {
            'extend_info': extend_info,
            'name': name,
            'type': type
        };
        var ep = new EventProxy();
        ep.fail(function (err) {
            cb(err);
        });
        ep.after('get_product_info', doc.product_configs.length, function () {
            goodsInfo['productsInfo'] = productsInfo;
            cb(null, goodsInfo);
        });
        doc.product_configs.map(function (productConfig) {
            var id = productConfig;
            ProductConfig.findOne({_id: id}, function (err, data) {
                if (err) {
                    cb(err);
                }
                Product.find({config: id}, function (error, product) {
                    if (error) {
                        cb(err);
                    }
                    if (product.length > 0) {
                        var product_version = data.version;
                        var product_name = data.name;
                        var product_type = data.type;

                        var file = product[0]._doc.url;
                        var file_path;
                        var product_url = url.parse(file);
                        switch (product_url.protocol) {
                            case 'file:':
                            {
                                file_path = product_url.path;
                                break;
                            }
                            default:
                                file_path = product_url.path;
                        }
                        productsInfo.push({
                            'type': product_type,
                            'version': product_version,
                            'name': product_name,
                            //'detail':product_detail,
                            'file': file_path
                        });
                        ep.emit('get_product_info');
                    }else{
                        cb({err:"发布失败：没有包含任何产品"});
                    }
                }).sort({timestamp: -1})
            });
        });
    });
};
